﻿using System;
using System.Collections;
using QGMiniGame;
using UnityEngine;

public class AdManager
{
    public static bool TestMode = false;

    public static string Owner = "游戏著作人：北京玩伴同乐科技有限公司"; //健康忠告-著作权人
    public static string License = "登记号：2023SR1484829"; //健康忠告-登记号
    public static int AgeLimit = 12; //健康忠告-适龄 8 12 16

    public static string Company = "厦门格拇科技有限公司"; //隐私-公司
    public static string Email = "客服邮箱：1018954230@qq.com"; //隐私-联系方式

    public static int Year = 2024;
    public static int Month = 1;
    public static int Day = 23;
    public static int Hour = 19;

    private static string AppId = "105712532";
    private static string VideoId = "830d49de98844f39a062c2532e40817a";
    private static string CustomId = "c8d82d5007f34bd690ab10354331e143";
    private static string BannerId = "3491ca0bc50746f086c87c88c0f601e9";

    public static void ShowBanner()
    {
        if (TestMode) return;
#if UNITY_EDITOR

#elif UNITY_ANDROID
#else
        ShowQGBanner();
#endif
    }

    public static void ShowNative()
    {
        if (TestMode) return;
        Debug.Log("弹出原生广告");
#if UNITY_EDITOR

#elif UNITY_ANDROID
#else
        ShowQGNative();
#endif
    }

    public static void ShowVideo(Action action)
    {
        if (TestMode)
        {
            action?.Invoke();
            return;
        }

#if UNITY_EDITOR
        action?.Invoke();
        return;
#elif UNITY_ANDROID
#else
        ShowQGVideo(action);
#endif
    }

    public static void Quit()
    {
#if UNITY_EDITOR
        GameManager.Instance.Quit();
#elif UNITY_ANDROID
#else
        QGMiniGameManager.Instance.Quit();
#endif
    }

    #region Android

    private static AndroidJavaClass jc;
    private static AndroidJavaObject jo;

    private static void CheckNull()
    {
        if (jc == null || jo == null)
        {
            jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
        }
    }

    private static void ShowAndroidBanner()
    {
        CheckNull();
        jo.Call("ShowBanner");
    }

    private static void ShowAndroidNative()
    {
        CheckNull();
        jo.Call("ChaPing");
    }

    private static void ShowAndroidVideo(Action action)
    {
        jo.Call("GameVideo1", action);
    }

    private static void ShowAndroidKeFu() => jo.Call("KeFu");

    private static void ShowAndroidMoreGame() => jo.Call("MoreGame");

    private static void ShowAndroidPrivacy() => jo.Call("Hide");

    private static void AndroidQuitGame() => jo.Call("Quit");

    #endregion

    #region VIVO

    /// <summary>Banner 广告</summary>
    private static void ShowQGBanner()
    {
        if (ZTool.InTimeRange(Year, Month, Day, Hour)) return;

        var bannerAd = QG.CreateBannerAd(new QGCreateBannerAdParam
            { adIntervals = 30, posId = BannerId });

        bannerAd.OnLoad(() =>
        {
            bannerAd.Show(
                (msg) => { Debug.Log("QG.bannerAd.Show success = " + JsonUtility.ToJson(msg)); },
                (msg) => { Debug.Log("QG.bannerAd.Show fail = " + msg.errMsg); }
            );
        });
        bannerAd.OnError((QGBaseResponse msg) =>
        {
            Debug.Log("QG.bannerAd.OnError success = " + JsonUtility.ToJson(msg));
        });
    }

    /// <summary>原生广告</summary>
    public static void ShowQGNative()
    {
        if (ZTool.InTimeRange(Year, Month, Day, Hour)) return;

        var customAd = QG.CreateCustomAd(new QGCreateCustomAdParam()
        {
            posId = CustomId
        });
        customAd.OnLoad(() =>
        {
            Debug.Log("QG.customAd.OnLoad success = ");
            customAd.Show(
                (msg) => { Debug.Log("QG.customAd.Show success = " + JsonUtility.ToJson(msg)); },
                (msg) => { Debug.Log("QG.customAd.Show fail = " + msg.errMsg); }
            );
        });
        customAd.OnError((QGBaseResponse msg) =>
        {
            Debug.Log("QG.customAd.OnError success = " + JsonUtility.ToJson(msg));
        });
        customAd.OnHide(() => { Debug.Log("QG.customAd.OnHide success "); });
    }

    /// <summary>视频广告</summary>
    public static void ShowQGVideo(Action action)
    {
        var rewardedVideoAd = QG.CreateRewardedVideoAd(new QGCommonAdParam { posId = VideoId });

        rewardedVideoAd.OnLoad(() =>
        {
            rewardedVideoAd.Show(
                (msg) => { Debug.Log("QG.rewardedVideoAd.Show success = " + JsonUtility.ToJson(msg)); },
                (msg) => { Debug.Log("QG.rewardedVideoAd.Show fail = " + msg.errMsg); }
            );
        });
        rewardedVideoAd.OnError((QGBaseResponse msg) =>
        {
            Tip.Show("视频广告加载失败");
            Debug.Log("QG.rewardedVideoAd.OnError success = " + JsonUtility.ToJson(msg));
        });
        rewardedVideoAd.OnClose((QGRewardedVideoResponse msg) =>
        {
            if (msg.isEnded)
            {
                action?.Invoke();
                Debug.Log("QG.rewardedVideoAd.OnClose success = " + " 播放成功");
            }
        });
    }

    #endregion

    // #region OPPO
    //
    // /// <summary>Banner 广告</summary>
    // private static void ShowQGBanner()
    // {
    //     if (ZTool.InTimeRange(Year, Month, Day, Hour)) return;
    //
    //     var bannerAd = QG.CreateBannerAd(new QGCreateBannerAdParam()
    //     {
    //         adUnitId = BannerId
    //     });
    //     Debug.Log("创建Banner广告开始运行");
    //     bannerAd.OnLoad(() => { Debug.Log("banner广告加载成功"); });
    //     bannerAd.OnError((QGBaseResponse msg) => { Debug.Log("QG.bannerAd.OnError success = " + JsonUtility.ToJson(msg)); });
    // }
    //
    // /// <summary>原生广告</summary>
    // public static void ShowQGNative()
    // {
    //     if (ZTool.InTimeRange(Year, Month, Day, Hour)) return;
    //
    //     var customAd = QG.CreateCustomAd(new QGCreateCustomAdParam()
    //     {
    //         adUnitId = CustomId //上文下图
    //     });
    //     Debug.Log("创建原生模板广告开始运行");
    //     customAd.OnLoad(() => { Debug.Log("原生模板广告加载成功"); });
    //     customAd.Show(
    //         (msg) => { Debug.Log("原生模板广告展示成功 = " + JsonUtility.ToJson(msg)); },
    //         (msg) => { Debug.Log("原生模板广告展示失败 = " + msg.errMsg); }
    //     );
    //     customAd.OnError((QGBaseResponse msg) => { Debug.Log("QG.customAd.OnError success = " + JsonUtility.ToJson(msg)); });
    //     customAd.OnHide(() => { Debug.Log("QG.customAd.OnHide success "); });
    // }
    //
    // /// <summary>视频广告</summary>
    // public static void ShowQGVideo(Action action)
    // {
    //     var rewardedVideoAd = QG.CreateRewardedVideoAd(new QGCommonAdParam()
    //     {
    //         adUnitId = VideoId
    //     });
    //     Debug.Log("创建激励视频开始运行");
    //     rewardedVideoAd.OnLoad(() =>
    //     {
    //         Debug.Log("激励视频广告加载成功");
    //         rewardedVideoAd.Show();
    //     });
    //     rewardedVideoAd.OnError((QGBaseResponse msg) => { Debug.Log("QG.rewardedVideoAd.OnError success = " + JsonUtility.ToJson(msg)); });
    //     rewardedVideoAd.OnClose((QGRewardedVideoResponse msg) =>
    //     {
    //         if (msg.isEnded)
    //         {
    //             action?.Invoke();
    //             Debug.Log("激励视频广告完成，发放奖励");
    //         }
    //         else
    //         {
    //             Debug.Log("激励视频广告取消关闭，不发放奖励");
    //         }
    //     });
    // }
    //
    // #endregion
}